package com.xiaoai.httpClientTestService.httpclient.javaf;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

public class HttpClientPoolT {

    public static void main(String[] args) throws Exception {
        // 创建连接池管理器
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

        // 设置连接池配置,
        // 最大连接数，即连接池中客户端数量
        cm.setMaxTotal(100);
        // 每个主机最大连接数 ，即每个主机最多连接的客户端数量
        cm.setDefaultMaxPerRoute(10);

        // 使用连接池发送请求，通过debug可查看每次生成的httpClient都不同
        doGet(cm);
        doPost(cm);

        // ----带请求配置
        //设置请求配置
        RequestConfig requestConfig = RequestConfig.custom()
                //连接超时时间
                .setConnectTimeout(30, TimeUnit.SECONDS)
                //连接请求超时时间
                .setConnectionRequestTimeout(10,TimeUnit.SECONDS)
                //响应超时时间
                .setResponseTimeout(10,TimeUnit.SECONDS)
                .build();

        doGet(cm,requestConfig);
        doPost(cm,requestConfig);

    }

    private static void doGet(PoolingHttpClientConnectionManager cm) throws Exception {

        // 从连接池管理器中获取客户端
        HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(cm);
        CloseableHttpClient httpClient = httpClientBuilder.build();

        HttpGet httpGet = new HttpGet("http://localhost:8080/test/getpGet");
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if (response.getCode() == 200){
            String content = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
            System.out.println(content);
        }
        //关闭响应
        response.close();
        //使用连接池，不需要自己关闭httpClient连接
//        httpClient.close();
    }

    private static void doPost(PoolingHttpClientConnectionManager cm) throws Exception {
        // 从连接池管理器中获取客户端
        HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(cm);
        CloseableHttpClient httpClient = httpClientBuilder.build();

        HttpPost httpPost = new HttpPost("http://localhost:8080/test/getpPost");
        CloseableHttpResponse response = httpClient.execute(httpPost);
        if (response.getCode() == 200){
            String content = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
            System.out.println(content);
        }
        //关闭响应
        response.close();
        //使用连接池，不需要自己关闭httpClient连接
//        httpClient.close();
    }


    private static void doGet(PoolingHttpClientConnectionManager cm, RequestConfig requestConfig) throws Exception {
        // 从连接池管理器中获取客户端
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(cm)
                .setDefaultRequestConfig(requestConfig)
                .build();

        HttpGet httpGet = new HttpGet("http://localhost:8080/test/getpGet");
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if (response.getCode() == 200){
            String content = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
            System.out.println(content);
        }
        //关闭响应
        response.close();
        //使用连接池，不需要自己关闭httpClient连接
//        httpClient.close();

    }

    private static void doPost(PoolingHttpClientConnectionManager cm, RequestConfig requestConfig)throws Exception {

        // 从连接池管理器中获取客户端
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(cm)
                .build();

        HttpPost httpPost = new HttpPost("http://localhost:8080/test/getpPost");
        httpPost.setConfig(requestConfig); //连接池不设置请求配置，在请求这里也可以设置
        CloseableHttpResponse response = httpClient.execute(httpPost);
        if (response.getCode() == 200){
            String content = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
            System.out.println(content);
        }
        //关闭响应
        response.close();
        //使用连接池，不需要自己关闭httpClient连接
//        httpClient.close();

    }

}
